<template>
  <div class="container">
    <div class="top">
      <h3 class="title"><span />模板文件</h3>
      <el-button size="small" type="primary" icon="el-icon-arrow-left" class="back" @click="prev">返回</el-button>
    </div>
    <el-table
      :data="themeData"
      :header-cell-style="{'text-align':'center'}"
      :cell-style="{'text-align':'center'}"
      style="margin-top:36px"
    >
      <el-table-column label="ID" prop="id" width="80px" />
      <el-table-column label="名称" prop="name" />
      <el-table-column label="操作名" prop="action" />
      <el-table-column label="文件路径" prop="file">
        <template slot-scope="scope">
          {{ scope.row.file }}
        </template>
      </el-table-column>
      <el-table-column label="描述" prop="description" />
      <el-table-column fixed="right">
        <template slot-scope="scope">
          <el-button class="edit" size="mini" icon="el-icon-edit" @click="goEdit(scope.row)">编辑</el-button>
          <el-button type="primary" class="blue" size="mini" icon="el-icon-refresh" @click="goRefresh(scope.row)">更新</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page.currentPage"
      :page-sizes="page.pageSizes"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
      :hide-on-single-page="page.total<page.size?true:false"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <OnlineEditor
      :dialog-show.sync="dialogShow"
      :editor-content="tempForm.content"
      :editor-type="fileType"
      :curr-path="currEditorPath"
      :width="80"
    />
  </div>

</template>

<script>
import themeApi from '@/api/theme'
import mouldApi from '@/api/theme'
export default {
  data() {
    return {
      fileType: 'html',
      currEditorPath: '',
      website_id: '',
      theme_id: '',
      theme: '',
      themeData: [],
      dialogShow: false,
      tempForm: {
        content: '',
        path: ''
      },
      tempRules: {
        content: [
          { required: 'true', message: '内容不能为空', tigger: 'blur' }
        ]
      },
      lang: '',
      themeName: '',
      num: 0,
      page: {
        total: 0,
        currentPage: 1,
        size: 10,
        pageSizes: [10, 20, 30, 40]
      } // 分页器
    }
  },
  created() {
    this.website_id = this.$route.query.id
    this.theme = this.$route.query.tname
    this.theme_id = this.$route.query.tid
    this.lang = this.$route.query.lang
    this.getList()
  },
  methods: {
    getList() {
      const val = {
        website_id: this.website_id,
        theme: this.theme,
        theme_id: this.theme_id,
        page: this.page.currentPage,
        limit: this.page.size,
        lang: this.lang
      }
      themeApi.tmpFile(val).then(res => {
        this.themeData = res.data
        this.page.total = res.count
      })
    },
    // 编辑
    goEdit(row) {
      this.tempForm.path = row.real_path
      this.fileType = this.getFileExt(row.real_path)
      this.currEditorPath = this.tempForm.path
      mouldApi.fileRead({ path: this.tempForm.path }).then(res => {
        this.tempForm.content = res.data
        this.dialogShow = true
      })
    },
    getFileExt(path) {
      return path.substr(path.lastIndexOf('.') + 1)
    },
    // 更新
    goRefresh(row) {
      this.tempForm.path = row.real_path
      themeApi.singleUpdate({ path: this.tempForm.path }).then(res => {
        if (res.code == 0) {
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    submit() {
      themeApi.fileUpdate({ ...this.tempForm }).then(res => {
        if (res.code == 0) {
          this.$message.success(res.msg)
          this.dialogShow = false
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 分页规格改变
    handleSizeChange(val) {
      this.page.size = val
      this.page.currentPage = 1
      this.getList()
    },
    // 分页点击
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.getList()
    },
    prev() {
      this.$router.push({ name: 'Theme' })
    }
  }
}
</script>
<style lang='scss' scoped>
#file{
  .el-dialog{
    background-color: #282722;
  }
  .el-dialog__header{
    padding: 10px 20px 1px;
    .el-dialog__title{
      color: #fff;
      font-size: 16px;
      // font-weight: bold;
    }
  }
  .el-dialog__body{
    padding: 0;
  }
  .save.el-button--primary{
    width: 54px;
    height: 25px;
    border-color: #565656;
    background-color: #565656;
    font-size: 12px;
  }
  .btn{
    margin-top: 0;
    margin-bottom: 10px;
    margin-left: 15px;
    text-align: left;
  }
}
</style>
<style scoped lang='scss'>
.top{
    display: flex;
    justify-content: left;
    align-items: center;
    .back{
        top: 16px;
        right: 21px;
    }
}
.el-table{
    margin-bottom: 20px;
}

</style>
